package com.books.controller.index;


import com.books.model.entity.book.Ebook;
import com.books.service.book.IEbookService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexTwoController {

    @Autowired(required = false)
    private IEbookService EbookService;

//    @Autowired(required = false)
//    private RedisTemplate redisTemplate;

    @GetMapping("/index02")
    public String index01Book(HttpServletRequest request, String name, Integer bookTypeId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "8") Integer pageSize)
    {
        PageInfo<Ebook> epageInfo1;
        PageInfo<Ebook> epageInfo2;
        PageInfo<Ebook> epageInfo4;
        PageInfo<Ebook> epageInfo6;
        PageInfo<Ebook> epageInfo7;
        PageInfo<Ebook> epageInfo8;
        PageInfo<Ebook> epageInfo9;
        epageInfo1 = EbookService.index(name,1,pageNum,pageSize);
        epageInfo2 = EbookService.index(name,2,pageNum,pageSize);
        epageInfo4 = EbookService.index(name,4,pageNum,pageSize);
        epageInfo6 = EbookService.index(name,6,pageNum,pageSize);
        epageInfo7 = EbookService.index(name,7,pageNum,pageSize);
        epageInfo8 = EbookService.index(name,8,pageNum,pageSize);
        epageInfo9 = EbookService.index(name,9,pageNum,pageSize);
//        if(redisTemplate.opsForValue().get("epageInfo1")==null){
//            epageInfo1 = EbookService.index(name,1,pageNum,pageSize);
//            redisTemplate.opsForValue().set("epageInfo1",epageInfo1,1, TimeUnit.DAYS);
//        }
//        else {
//            epageInfo1= (PageInfo<Ebook>) redisTemplate.opsForValue().get("epageInfo1");
//        }
//        if(redisTemplate.opsForValue().get("epageInfo2")==null){
//            epageInfo2 = EbookService.index(name,2,pageNum,pageSize);
//            redisTemplate.opsForValue().set("epageInfo2",epageInfo2,1, TimeUnit.DAYS);
//        }
//        else {
//            epageInfo2= (PageInfo<Ebook>) redisTemplate.opsForValue().get("epageInfo2");
//        }
//        if(redisTemplate.opsForValue().get("epageInfo4")==null){
//            epageInfo4 = EbookService.index(name,4,pageNum,pageSize);
//            redisTemplate.opsForValue().set("epageInfo4",epageInfo4,1, TimeUnit.DAYS);
//        }
//        else {
//            epageInfo4= (PageInfo<Ebook>) redisTemplate.opsForValue().get("epageInfo4");
//        }
//        if(redisTemplate.opsForValue().get("epageInfo6")==null){
//            epageInfo6 = EbookService.index(name,6,pageNum,pageSize);
//            redisTemplate.opsForValue().set("epageInfo6",epageInfo6,1, TimeUnit.DAYS);
//        }
//        else {
//            epageInfo6= (PageInfo<Ebook>) redisTemplate.opsForValue().get("epageInfo6");
//        }
//        if(redisTemplate.opsForValue().get("epageInfo7")==null){
//            epageInfo7 = EbookService.index(name,7,pageNum,pageSize);
//            redisTemplate.opsForValue().set("epageInfo7",epageInfo7,1, TimeUnit.DAYS);
//        }
//        else {
//            epageInfo7= (PageInfo<Ebook>) redisTemplate.opsForValue().get("epageInfo7");
//        }
//        if(redisTemplate.opsForValue().get("epageInfo8")==null){
//            epageInfo8 = EbookService.index(name,8,pageNum,pageSize);
//            redisTemplate.opsForValue().set("epageInfo8",epageInfo8,1, TimeUnit.DAYS);
//        }
//        else {
//            epageInfo8= (PageInfo<Ebook>) redisTemplate.opsForValue().get("epageInfo8");
//        }
//        if(redisTemplate.opsForValue().get("epageInfo9")==null){
//            epageInfo9 = EbookService.index(name,9,pageNum,pageSize);
//            redisTemplate.opsForValue().set("epageInfo9",epageInfo9,1, TimeUnit.DAYS);
//        }
//        else {
//            epageInfo9= (PageInfo<Ebook>) redisTemplate.opsForValue().get("epageInfo9");
//        }
        request.setAttribute("pageInfo1",epageInfo1);
        request.setAttribute("pageInfo2",epageInfo2);
        request.setAttribute("pageInfo4",epageInfo4);
        request.setAttribute("pageInfo6",epageInfo6);
        request.setAttribute("pageInfo7",epageInfo7);
        request.setAttribute("pageInfo8",epageInfo8);
        request.setAttribute("pageInfo9",epageInfo9);
        return "index02";
    }
}
